<?php

/**
 * Description of ExtJSMergeProvider
 *
 * @author polacseka
 */
class XDD_Manager_ExtJS_MergeProvider extends XDD_Provider
{
	public $xName = 'Szolgáltató szervezett elrendezésű JS fájlok összeépítéséhez az ExtJS számára';

	public $contentType = 'application/x-javascript; charset=utf-8';

	protected $content = '';

	protected $rootFolderPath;

	protected $fileExtLayout = '.layout.js';

	protected $fileExtWindow = '.window.js';

	protected $fileExtMethods = '.methods.js';

	public function __construct(x10_Config $config=NULL)
	{
		parent::__construct($config);

		$this->rootFolderPath = x10::$systemRoot.$this->config->getConfigKey('rootFolderPath');
	}

	public function init()
	{
		$data = $this->config->getConfigKey('data');
		if (!is_null($data)) $this->data = $data;
		
		$le = $this->config->getConfigKey('fileExtMethods');
		if (!is_null($le)) $this->fileExtMethods = $le;

		$le = $this->config->getConfigKey('fileExtLayout');
		if (!is_null($le)) $this->fileExtLayout = $le;
		
		$ce = $this->config->getConfigKey('fileExtWindow');
		if (!is_null($ce)) $this->fileExtWindow = $ce;
	}

	public function __toString()
	{
		$toMerge = array();
		$files = x10_Util::globr($this->rootFolderPath.'*'.$this->fileExtMethods);
		if (is_array($files))
		{
			foreach ($files as $file) $toMerge[] = $file;
		}
		$files = x10_Util::globr($this->rootFolderPath.'*'.$this->fileExtLayout);
		if (is_array($files))
		{
			foreach ($files as $file) $toMerge[] = $file;
		}
		$files = x10_Util::globr($this->rootFolderPath.'*'.$this->fileExtWindow);
		if (is_array($files))
		{
			foreach ($files as $file) $toMerge[] = $file;
		}

		foreach ($toMerge as $file) $this->content .= file_get_contents($file);

		return 'x10Data = '.json_encode($this->data).';'."\n\n".$this->content;
	}
}

?>